<?php

class SiteAction extends CommonAction
{
    
	public function index()
    {
		$this->assign("current_site_index",'current');
		$this->display();
    }
	
	function addSite()
	{				
		$this->assign("current_site_add",'current');
		$this->assign("siteinfo",$_SESSION['siteinfo']);
		$this->display();	
	}
	function doAddSite(){
	
		if(!empty($_POST['install'])){
			foreach($_POST as $key =>$val)
			{
				$_SESSION['siteinfo'][$key] = $val;
			}
		}
		else{
			echo '2'; 
		}
        exit;
	}
	function addSite2()
	{				
		$this->assign("current_site_add",'current');	
		$this->display();	
	}
	function doAddSite2(){

        $host = M('host'); 
       	$data['user_id'] = $_SESSION['user_id'];
        
        $data['host_site_url']  = $_SESSION['siteinfo']['site_url'];
        $data['host_site_title']  = $_SESSION['siteinfo']['site_title'];
        
		$data['host_backend_admin']  = $_SESSION['siteinfo']['host_username'];	
		$data['host_backend_psw']  = $_SESSION['siteinfo']['host_psw'];
		$data['host_backend_email']  = $_SESSION['siteinfo']['host_address'];
        
		$data['host_address']   = $_SESSION['siteinfo']['database_url'];
		$data['host_dbname']   = $_SESSION['siteinfo']['database_name'];
		$data['host_dbuser']   = $_SESSION['siteinfo']['database_username'];		
		$data['host_dbpsw']    = $_SESSION['siteinfo']['database_psw'];
        
        $data['host_address']    = $_SESSION['siteinfo']['ftp_dir'];
        $data['host_name']    = $_SESSION['siteinfo']['ftp_user'];
        $data['host_psw']    = $_SESSION['siteinfo']['ftp_psw'];
        $data['host_install']    = $_SESSION['siteinfo']['install'];
        
        $data['host_site_language']    = "Zh_CN";
        $data['host_site_system']    = "WordPress";
        $data['host_site_theme']    = "ttway";
        $data['host_site_plugin']    = "seo";
        $data['host_publish']    = "1";
        
        $rel = $host->data($data)->add();
        if($rel){
    		import("@.ORG.ftp");
            $row = $host->where("host_id =".$rel)->find(); 
            $uploads = $row['host_install'];
            $new_file = makeActivationCode(6).'.php';
            $path = C("path");
            $config = array(
            			'hostname' => $row['host_address'],
            			'username' => $row['host_name'],
            			'password' => $row['host_psw'],
            			'port' => 21
            				);         
            $ftp = new Ftp();
            $b = $ftp->connect($config);
            if($b)
            {
                $wp_file = 'wp.zip';
                $wp_path = $path.'/files/wp/'.$wp_file;
                $uncompress_wp = $path.'/files/wp/uncompress_wp.php';
                if(file_exists($wp_path))
                {
                    $a = $ftp->upload($wp_path,$uploads.'/wp.zip');
                    if($a)            
                    {    
                		$ftp->upload($uncompress_wp,$uploads.'/'.$new_file);
                		$url = 'http://demo.favortt.com/'.$new_file;
                		$ch = curl_init();
                		curl_setopt($ch, CURLOPT_URL, $url);
                		curl_setopt($ch, CURLOPT_HEADER, false);
                		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                		curl_setopt($ch, CURLOPT_FAILONERROR, false);
                		$result = curl_exec($ch);
                		curl_close($ch);	
                		echo $result;
                        exit;
                	}
                    else
                    {    
                        echo '2';
                		exit;
                    }
                  } 	
            }
        }
        else{
            echo '保存失败';
        }        
        exit;
	}
	function uploadPlguin(){
	   
       echo '1';
       exit;
	}
	function siteOption()
	{
		$this->assign("current_site_opt",'current');
		//$this->assign("checkshow","1");
		$this->display();	
	}
		
	function ftpTree()
	{			
		
		if(empty($_POST['ftp_dir']) || empty($_POST['ftp_user']) || empty($_POST['ftp_psw']) )
		{
			die('1');
		}
		else
		{
			$conn = ftp_connect($_POST['ftp_dir'], 21, 90); 
			if(!$conn)
			{
			  die('1');
			}
			else
			{
				$FtpLogin = ftp_login($conn, $_POST['ftp_user'], $_POST['ftp_psw']);
				if(!$FtpLogin)
				{
					die('1');
				}
				else
				{
					$files = ftp_nlist($conn,'.');
				}
				$html_tree =  '<div id="demo3" class="demo" style="padding:10px 0; margin:0px 0; " ><ul>';
				foreach($files as $k => $file)    //根目录
				{
				  $thisFile = basename($file);
				  if($thisFile != '.' && $thisFile != '..')
				  {
						$dr1 = substr($thisFile,0,1);
						$loc = strpos($thisFile,'.');
						if(empty($loc) && $dr1 != '.' )
						{			
							//@mkdir($file, 0777, true);  //在本地创建FTP文件目录
							$newdir = @ftp_nlist($conn, './'.$file);
							if(!empty($newdir)){ $op = 'jstree-open'; }
							$html_tree .= '<li rel="./'.$file.'" class="'.$op.'">';
							$html_tree .= '<a href="#">'.$thisFile.'</a>';  
							if(!empty($newdir))
							{
								$html_tree .= '<ul>';
								foreach($newdir as $key=>$vol)    //二级目录
								{
										$thisFile2 = basename($vol);
										if($thisFile2 != '.' && $thisFile2 != '..')
										{
											$dir2 = substr($thisFile2,0,1);
											$loc2 = strpos($thisFile2,'.');
											if(empty($loc2) && $dir2 != '.' )
											{
												//@mkdir($vol, 0777, true); //在本地创建FTP文件目录
												$replace2 =  str_replace('\\',"/",$vol);
												$sub2 = substr($vol,0,1);
												if($sub2 == '.')
												{
													$html_tree .= '<li rel="'.$replace2.'">';
												}
												else
												{
													$html_tree .= '<li rel="./'.$replace2.'">';
												}
												$html_tree .= '<a href="#">'.$thisFile2.'</a>';
												$newdir2 = @ftp_nlist($conn, './'.$vol);  //判断
												if(!empty($newdir2))
												{
													$html_tree .= '<ul>';
													foreach($newdir2 as $key2=>$vol2)    //三级目录
													{
														$thisFile3 = basename($vol2);
														if($thisFile3 != '.' && $thisFile3 != '..')
														{
															$dir3 = substr($thisFile3,0,1);
															$loc3 = strpos($thisFile3,'.');
															if(empty($loc3) && $dir3 != '.' )
															{
																$sub3 = substr($vol2,0,1);
																if($sub3 == '.')
																{
																	$replace3 =  substr(str_replace('\\',"/",$vol2),2);
																	$html_tree .= '<li rel="'.$replace3.'">';
																}
																else
																{
																	$replace3 = str_replace('\\',"/",$vol2);
																	$html_tree .= '<li rel="./'.$replace3.'">';
																}
																$html_tree .= '<a href="#">'.$thisFile3.'</a>';
																$html_tree .= '</li>';
																//@mkdir($vol2, 0777, true); //在本地创建FTP文件目录
															}
														}
													}
													$html_tree .= '</ul>';
												}
												$html_tree .= '</li>';
											}
									}
								}
								$html_tree .= '</ul>';
							}
						}
				  }
				}
								$html_tree .= '</li>';
								$html_tree .= '</ul></div>';
								ftp_close($conn);
								echo $html_tree.'<script>
								$(function () {
								$("#demo3").jstree({ "plugins" : ["themes","html_data","ui"] })
								.bind("select_node.jstree", function (event, data) {
									
									 var data = data.rslt.obj.attr("rel");
									 var v = $("#install").attr("value",data);
								})
								.delegate("a", "click", function (event, data) { event.preventDefault(); });
								})
								</script>';
			}
		}
		exit;
		}
}
?>